<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible = true"
          >添加分类</el-button
        >
      </div>
      <!-- 文章分类图表 -->
      <el-table :data="ArtList" style="width: 100%" stripe border>
        <el-table-column label="序号" width="50" type="index">
        </el-table-column>
        <el-table-column prop="cate_name" label="分类名称" width="397">
        </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column label="操作">
            <!-- 作用域插槽 scoped.row -->
          <template v-slot="scoped">
            <el-button type="primary" size="mini" @click="edit(scoped.row.id)">修改</el-button>
            <el-button type="danger" size="mini" @click="del(scoped.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 点击添加文章分类的dialog -->
    <el-dialog
      title="添加文章分类"
      :visible.sync="addVisible"
      width="35%"
      :before-close="handleClose"
      @closed="$refs.ArtForm.resetFields()"
    >
      <el-form
        :model="ArtForm"
        :rules="Artrules"
        ref="ArtForm"
        label-width="70px"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="ArtForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="ArtForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false" size="mini">取 消</el-button>
        <el-button type="primary" @click="addCate" size="mini">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 点击修改文章分类的dialog -->
    <el-dialog
      title="添加文章分类"
      :visible.sync="editVisible"
      width="35%"
      :before-close="handleClose"
      @closed="$refs.editForm.resetFields()"
    >
      <el-form
        :model="editForm"
        :rules="editRules"
        ref="editForm"
        label-width="70px"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false" size="mini">取 消</el-button>
        <el-button type="primary" size="mini" @click="editCate">修 改</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data () {
    return {
      ArtList: [],
      ArtForm: {
        cate_name: '',
        cate_alias: ''
      },
      editForm: {
        cate_name: '',
        cate_alias: ''
      },
      Artrules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '请输入1~10位非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '请输入1~15位字母数字', trigger: 'blur' }
        ]
      },
      editRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '请输入1~10位非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '请输入1~15位字母数字', trigger: 'blur' }
        ]
      },
      addVisible: false,
      editVisible: false
    }
  },
  methods: {
    // 获取最新分类列表
    async getAriCate () {
      // 发送axios请求
      const { data: res } = await this.$http.get('/my/cate/list')
      // console.log(res)
      this.ArtList = res.data
    },
    // 点击确定添加分类
    addCate () {
      // 兜底校验
      this.$refs.ArtForm.validate(async vali => {
        if (!vali) return
        // 发送请求，添加分类
        const { data: res } = await this.$http.post('/my/cate/add', this.ArtForm)
        // console.log(res)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 关闭dialog
        this.addVisible = false
        // 获取最新分类
        this.getAriCate()
      })
    },
    // 点击修改文章分类
    async edit (id) {
      this.editVisible = true
      const { data: res } = await this.$http.get('/my/cate/info', {
        params: {
          id
        }
      })
      // console.log(res)
      this.editForm = res.data
    },
    // 确认修改
    editCate () {
      // 兜底校验
      this.$refs.editForm.validate(async vali => {
        if (!vali) return
        // 发送请求
        const { data: res } = await this.$http.put('/my/cate/info', this.editForm)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 获取最新数据
        this.getAriCate()
        // 关闭dialog
        this.editVisible = false
      })
    },
    // 点击删除
    del (id) {
      this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.delete('/my/cate/del', {
          params: { id }
        })
        this.$message.success(res.message)
        this.getAriCate()
      }).catch(() => {
      })
    }
  },
  created () {
    this.getAriCate()
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
